package com.ctgu.fcftsystem.controller;

import com.ctgu.fcftsystem.pojo.Securities;
import com.ctgu.fcftsystem.pojo.Sercurity_trade;
import com.ctgu.fcftsystem.service.SecuritiesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/SecuritiesController")
public class SecuritiesController {
    @Autowired
    private SecuritiesService securitiesService;


    @RequestMapping("/findByPaging")
    public Object findByPaging(int pageCount,int pageSize){
        ModelMap map = new ModelMap();
        PageHelper.startPage(pageCount, pageSize);//设置数据库分页查询的范围
        try {
            List<Securities> userList = securitiesService.findByPaging(pageCount, pageSize);
            //分页的信息
            PageInfo<Securities> pageInfo=new PageInfo<>(userList);
            map.put("msg","查询所有角色成功");
            map.put("data",pageInfo);
            map.put("code",0);
        }catch (Exception e){
            e.getStackTrace();
            map.put("msg","查询所有角色失败");
            map.put("code",1);
        }
        return map;
    }

    /**
     * 添加证券用户
     * @param securitiesOwner
     * @param securitiesId
     * @param securitiesPassword
     * @param securitiesCompany
     * @param securitierType
     * @param starTime
     * @param endTime
     * @return
     */
    @RequestMapping("/addSecurities")
    public  Map  addSecurities(String securitiesOwner, String securitiesId, String securitiesPassword
            , String securitiesCompany, String securitierType, String starTime, String endTime){

        Map<String, Object> map = new HashMap<>();
        int flag;
        if((securitiesOwner!=null&&securitiesOwner.length()>0)&&(securitiesId!=null&&securitiesId.length()>0)&&(securitiesPassword!=null&&securitiesPassword.length()>0)
                &&(securitiesCompany!=null&&securitiesCompany.length()>0) &&(securitierType!=null&&securitierType.length()>0)
                &&(starTime!=null&&starTime.length()>0)&&(endTime!=null&&endTime.length()>0)) {


            Securities securities = new Securities();
            securities.setSecuritiesOwner(securitiesOwner);
            securities.setSecuritiesId(securitiesId);
            securities.setSecuritiesPassword(securitiesPassword);
            securities.setSecuritiesCompany(securitiesCompany);
            securities.setSecuritierType(securitierType);
            securities.setStarTime(starTime);
            securities.setEndTime(endTime);
            flag = securitiesService.addSecurities(securities);

        }
        else{

            flag = -1;
        }


        if(flag > 0){
            map.put("code",0);
            map.put("msg","添加成功");
            map.put("data",flag);
        }else{
            map.put("code",1);
            map.put("msg","添加失败");
            map.put("data",flag);
        }

        return map;
    }

    /**
     * 根据持有人、证券公司、证券类型、起始时间和截止时间进行多重条件查询
     * @param securitiesOwner
     * @param securitiesCompany
     * @param securitierType
     * @param starTime
     * @param endTime
     * @return
     */
    @RequestMapping("/searchSecuritiesUser")
    public Map searchSecuritiesUser(String securitiesOwner, String securitiesCompany,  String securitierType
            , String starTime, String endTime){

         Map<String, Object> map = new HashMap<>();
         List<Securities> securitiesList = securitiesService.searchSecuritiesUser(securitiesOwner,
                 securitiesCompany, securitierType,starTime, endTime);
            if(securitiesList!=null&&securitiesList.size()>0){
                map.put("code",0);
                map.put("msg","查询成功");
                map.put("data",securitiesList);
            }else{
                map.put("code",1);
                map.put("msg","查询失败");
                map.put("data",-1);
            }
        return map;
    }

    /**
     * 根据id删除证券用户
     * @param id
     * @return
     */
    @RequestMapping("/deleteSecuritiesUsrById")
    public Map  deleteSecuritiesUsrById(Integer id){
        Map<String, Object> map = new HashMap<>();
        int flag ;
        if(id!=null&&id>0){

            flag = securitiesService.deleteSecuritiesUsrById(id);
        }else{
            flag = -1;
        }


        if(flag==1){
            map.put("code",0);
            map.put("msg","删除成功");
            map.put("data",flag);
        }else{
            map.put("code",1);
            map.put("msg","删除失败");
            map.put("data",flag);

        }

        return map;
    }

    /**
     * 根据id维护证券用户
     * @param id
     * @param securitiesOwner
     * @param securitiesId
     * @param securitiesPassword
     * @param securitiesCompany
     * @param securitierType
     * @param starTime
     * @param endTime
     * @return
     */
    @RequestMapping("/updateSecuritiesUsrById")
    public Map updateSecuritiesUsrById(Integer id, String securitiesOwner, String securitiesId, String securitiesPassword
            , String securitiesCompany, String securitierType,String starTime, String endTime){
        Map<String, Object> map = new HashMap();
            Securities securities = new Securities();
            int flag;
        if((id!=null&&id>0)&&((securitiesOwner!=null&&securitiesOwner.length()>0)||(securitiesId!=null&&securitiesId.length()>0)
                ||(securitiesPassword!=null&&securitiesPassword.length()>0)
                ||(securitiesCompany!=null&&securitiesCompany.length()>0)||(securitierType!=null&&securitierType.length()>0)
                ||(starTime!=null&&starTime.length()>0)||(endTime!=null&&endTime.length()>0))){
                securities.setId(id);
                securities.setSecuritiesOwner(securitiesOwner);
                securities.setSecuritiesId(securitiesId);
                securities.setSecuritiesPassword(securitiesPassword);
                securities.setSecuritiesCompany(securitiesCompany);
                securities.setSecuritierType(securitierType);
                securities.setStarTime(starTime);
                securities.setEndTime(endTime);
                flag = securitiesService.updateSecuritiesUsrById(securities);
            }else{
            flag = -1;
        }


            if(flag>0){
                map.put("code",0);
                map.put("msg","维护成功");
                map.put("data",flag);
            }else{
                map.put("code",1);
                map.put("msg","维护失败");
                map.put("data",-1);

            }
       return  map;
    }


}
